///*
// *    Copyright (c) 2018-2025, lengleng All rights reserved.
// *
// * Redistribution and use in source and binary forms, with or without
// * modification, are permitted provided that the following conditions are met:
// *
// * Redistributions of source code must retain the above copyright notice,
// * this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// * notice, this list of conditions and the following disclaimer in the
// * documentation and/or other materials provided with the distribution.
// * Neither the name of the pig4cloud.com developer nor the names of its
// * contributors may be used to endorse or promote products derived from
// * this software without specific prior written permission.
// * Author: lengleng (wangiegie@gmail.com)
// */
//package com.excesys.exsecs.om.service.impl;
//
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.io.IoUtil;
//import cn.hutool.core.util.IdUtil;
//import cn.hutool.core.util.StrUtil;
//import com.amazonaws.services.s3.model.S3Object;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.excesys.exsecs.common.core.util.R;
//import com.excesys.exsecs.common.file.oss.OssProperties;
//import com.excesys.exsecs.common.file.oss.service.OssTemplate;
//import com.excesys.exsecs.om.entity.OmOFile;
//import com.excesys.exsecs.om.mapper.OmOFileMapper;
//import com.excesys.exsecs.om.service.OmOFileService;
//import lombok.AllArgsConstructor;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletResponse;
//import java.time.Duration;
//import java.time.temporal.ChronoUnit;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 文件管理表
// *
// * @author weiyj
// * @date 2023-02-24 13:52:55
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class OmOFileServiceImpl extends ServiceImpl<OmOFileMapper, OmOFile> implements OmOFileService {
//
//	private final OssProperties ossProperties;
//
//	private final OssTemplate ossTemplate;
//
//	/**
//	 * 上传文件
//	 * @param file
//	 * @return
//	 */
//	@Override
//	public R uploadFile(MultipartFile file) {
//		String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
//		Map<String, String> resultMap = new HashMap<>(4);
//		resultMap.put("bucketName", ossProperties.getBucketName());
//		resultMap.put("fileName", fileName);
//		resultMap.put("original", file.getOriginalFilename());
//
//		resultMap.put("url", String.format("/om/omofile/%s/%s", ossProperties.getBucketName(), fileName));
//
//		try {
//			ossTemplate.putObject(ossProperties.getBucketName(), fileName, file.getContentType(),
//					file.getInputStream());
//			// 文件管理数据记录,收集管理追踪文件
//			fileLog(file, fileName, resultMap);
//		}
//		catch (Exception e) {
//			log.error("上传失败", e);
//			return R.failed(e.getLocalizedMessage());
//		}
//		return R.ok(resultMap);
//	}
//
//	/**
//	 * 读取文件
//	 * @param bucket
//	 * @param fileName
//	 * @param response
//	 */
//	@Override
//	public void getFile(String bucket, String fileName, HttpServletResponse response) {
//		try (S3Object s3Object = ossTemplate.getObject(bucket, fileName)) {
//			response.setContentType("application/octet-stream; charset=UTF-8");
//			IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
//		}
//		catch (Exception e) {
//			log.error("文件读取异常: {}", e.getLocalizedMessage());
//		}
//	}
//
//	/**
//	 * 删除文件
//	 * @param id
//	 * @return
//	 */
//	@Override
//	@SneakyThrows
//	@Transactional(rollbackFor = Exception.class)
//	public Boolean deleteFile(Long id) {
//		OmOFile file = this.getById(id);
//		ossTemplate.removeObject(ossProperties.getBucketName(), file.getFileName());
//		return this.removeById(id);
//	}
//
//	/**
//	 * 文件管理数据记录,收集管理追踪文件
//	 * @param file 上传文件格式
//	 * @param fileName 文件名
//	 */
//	private void fileLog(MultipartFile file, String fileName, Map<String, String> resultMap) {
//		OmOFile omOFile = new OmOFile();
//		omOFile.setFileName(fileName);
//		omOFile.setOriginal(file.getOriginalFilename());
//		omOFile.setFileSize(file.getSize());
//		omOFile.setType(FileUtil.extName(file.getOriginalFilename()));
//		omOFile.setBucketName(ossProperties.getBucketName());
//		this.save(omOFile);
//		resultMap.put("fileId", omOFile.getId().toString());
//	}
//
//	/**
//	 * 默认获取文件的在线地址
//	 * @param bucket
//	 * @param fileName
//	 * @return
//	 */
//	@Override
//	public String onlineFile(String bucket, String fileName) {
//		return ossTemplate.getObjectURL(bucket, fileName, Duration.of(7, ChronoUnit.DAYS));
//	}
//
//}
